/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.core.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import info.peigen.hotpot.business.account.facade.order.query.QueryAccountByCustomerIdOrder;
import info.peigen.hotpot.business.account.facade.result.query.QueryAccountResult;
import info.peigen.hotpot.business.customer.facade.CustomerAccountServiceFacade;
import info.peigen.hotpot.business.customer.facade.info.account.CustomerQueryAccountInfo;
import info.peigen.hotpot.business.customer.facade.order.account.CustomerQueryAccountLogOverviewOrder;
import info.peigen.hotpot.business.customer.facade.order.account.CustomerQueryAccountOrder;
import info.peigen.hotpot.business.customer.facade.result.account.CustomerQueryAccountLogOverviewResult;
import info.peigen.hotpot.business.customer.facade.result.account.CustomerQuerySingleAccountResult;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.ServiceQuery;
import info.peigen.hotpot.common.service.base.ServiceQueryContext;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Component;

/**
 * <b>(CustomerAccountService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/2/9
 */
@Slf4j
@Component
public class CustomerAccountService extends AbstractCustomerService implements CustomerAccountServiceFacade {
    @Override
    public CustomerQuerySingleAccountResult queryAccount(CustomerQueryAccountOrder order) {
        ServiceQueryContext serviceContext = ServiceQueryContext.builder().order(order).result(new CustomerQuerySingleAccountResult()).build();
        query(serviceContext, new ServiceQuery() {
            @Override
            public void query(ServiceQueryContext serviceContext) {
                CustomerQueryAccountOrder order = (CustomerQueryAccountOrder) serviceContext.getOrder();

                // 1. 接口查询
                QueryAccountByCustomerIdOrder queryAccountOrder  = QueryAccountByCustomerIdOrder.builder().customerId(order.getCustomerId()).build();
                QueryAccountResult            queryAccountResult = queryAccountServiceFacade.queryAccountByCustomerId(queryAccountOrder);

                // 2. 一系列校验,保障转换步骤可以完成
                Assert.notNull(queryAccountResult, () -> new BusinessException(ResultCode.FAILURE, "查询[账户信息]失败![customerId=" + order.getCustomerId() + "]"));
                Assert.isTrue(queryAccountResult.success(), () -> new BusinessException(ResultCode.FAILURE, "查询[账户信息]失败![customerId=" + order.getCustomerId() + "]"));
                Assert.notNull(queryAccountResult.getInfo(), () -> new BusinessException(ResultCode.FAILURE, "查询[账户信息]失败![customerId=" + order.getCustomerId() + "]"));
                serviceContext.setInfo(queryAccountResult.getInfo());
            }

            @Override
            public void convert(ServiceQueryContext serviceContext) {
                // 3. 组装Result
                ((CustomerQuerySingleAccountResult) serviceContext.getResult())
                        .fromSuccess(BeanUtil.copyProperties(serviceContext.getInfo(), CustomerQueryAccountInfo.class));
            }
        });
        return (CustomerQuerySingleAccountResult) serviceContext.getResult();
    }


    @Override
    public CustomerQueryAccountLogOverviewResult queryAccountLogOverview(CustomerQueryAccountLogOverviewOrder order) {
        return null;
    }

}
